//
//  RecordTimePickerViewController.m
//  智积分
//
//  Created by Cynic on 2018/8/6.
//  Copyright © 2018年 Cynic. All rights reserved.
//

#import "RecordTimePickerViewController.h"
//#import "QFDatePickerView.h"
#import "CXDataPickView.h"
@interface RecordTimePickerViewController ()<CustomDataPickViewDelegate>
@property (nonatomic ,retain)CXDataPickView *picker;
@property (nonatomic ,retain)UIButton *pickerView;
@property (nonatomic ,assign)NSInteger type;
@property (nonatomic ,retain)UIButton *pickerBtn;
@property (nonatomic ,retain)UIButton *leftTime;
@property (nonatomic ,retain)UIButton *rightTime;
@property (nonatomic ,retain)UIButton *centerTime;
@property (nonatomic ,retain)UIButton *currentTime;
//@property (nonatomic ,strong)UIView *disTimeView;
@end

@implementation RecordTimePickerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = bggrayColor;
    [self viewLoad];
    // Do any additional setup after loading the view.
}

-(void)viewLoad{
    [self setNavigationViewWithTitle:@"选择时间" WithCanPop:YES WithPopData:@"return" WithPopisString:NO WithHasRightOperation:YES WithrightOperationData:@"完成" WithRightisString:YES WithNavigationColor:BaseColor withBgView:self.view];
    [self chooseLoad];
}

-(void)chooseLoad{
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 44 + cxsingler.topdis, cx_wid, cx_hit - cxsingler.topdis - cxsingler.bottomdis - 44)];
    [self.view addSubview:bgView];
    
    
    //    [self FieldBtnLoad:bgView];
    
    NSArray *titles = @[@"今日",@"最近3天",@"最近7天",@"最近30天"];
    CGFloat sidedis = 30;
    CGFloat partwid = 70;
    CGFloat parthit = 30;
    CGFloat topdis = 20;
    CGFloat centerdis = (cx_wid - sidedis * 2 - partwid * titles.count) / (titles.count - 1);
    WEAKSELF;
    for (NSInteger index = 0; index < titles.count; index ++) {
        UIButton *partBtn = [[UIButton alloc] initWithFrame:CGRectZero];
        partBtn.backgroundColor = [[UIColor lightGrayColor] colorWithAlphaComponent:0.2];
        partBtn.layer.cornerRadius = parthit / 2;
        partBtn.layer.borderColor = [UIColor lightGrayColor].CGColor;
        partBtn.layer.borderWidth = 1;
        [partBtn setTitle:titles[index] forState:UIControlStateNormal];
        [partBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        partBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        __weak typeof(partBtn) weakBtn = partBtn;
        [partBtn cx_eventsBlockHandler:^{
            if (weakSelf.currentTime != weakBtn) {
                [weakSelf.currentTime setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            weakSelf.currentTime = weakBtn;
                [weakSelf.currentTime setTitleColor:[UIColor  orangeColor] forState:UIControlStateNormal];
            }
        } forControlEvents:UIControlEventTouchUpInside];
        [bgView addSubview:partBtn];
        [partBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(sidedis + (partwid + centerdis) * index);
            make.top.mas_equalTo(topdis);
            make.size.mas_equalTo(CGSizeMake(partwid, parthit));
        }];
    }
    
    _pickerBtn = [[UIButton alloc] initWithFrame:CGRectZero];
    _pickerBtn.backgroundColor = [[UIColor lightGrayColor] colorWithAlphaComponent:0.2];
    _pickerBtn.layer.borderWidth = 1;
    _pickerBtn.layer.borderColor = [UIColor lightGrayColor].CGColor;
    [_pickerBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    _pickerBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    _pickerBtn.layer.cornerRadius = parthit / 2;
    [_pickerBtn setTitle:@"按月选择>" forState:UIControlStateNormal];
    [bgView addSubview:_pickerBtn];
    [_pickerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(sidedis);
        make.top.mas_equalTo(topdis + parthit + topdis);
        make.size.mas_equalTo(CGSizeMake(120, parthit));
    }];
    [_pickerBtn cx_eventsBlockHandler:^{
        [weakSelf pickerChange];
    } forControlEvents:UIControlEventTouchUpInside];
    
    
    for (NSInteger index = 0; index < 2; index ++) {
        UIButton *partBtn = [[UIButton alloc] initWithFrame:CGRectZero];
        [self.view addSubview:partBtn];
        if (index == 0) {
            _leftTime = partBtn;
        }
        else if (index == 1){
            _rightTime = partBtn;
        }
        [partBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo((topdis + parthit) * 2 + parthit + 44 + cxsingler.topdis);
            if (index == 0) {
                make.left.mas_equalTo(20);
            }
            else if (index == 1){
                make.right.mas_equalTo(-20);
            }
            make.width.mas_equalTo(cx_wid / 2 - 20 - 20);
            make.height.mas_equalTo(30);
        }];
        [partBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        __weak typeof(partBtn) weakPartBtn = partBtn;
        //        [partBtn cx_eventsBlockHandler:^{
        //            [weakSelf pickerNow:weakPartBtn];
        //        } forControlEvents:UIControlEventTouchDragInside];
        [partBtn cx_eventsBlockHandler:^{
            [weakSelf pickerNow:weakPartBtn];
        } forControlEvents:UIControlEventTouchUpInside];
        //        [self.view loadLayerViewBaseView:partBtn withHit:1 withDistype:2 withColor:BaseColor];
    }
    
    
    _centerTime = [[UIButton alloc] initWithFrame:CGRectZero];
    
    [self.view addSubview:_centerTime];
    [_centerTime mas_makeConstraints:^(MASConstraintMaker *make) {
        //        make.left.top.right.bottom.mas_equalTo(0);
        make.top.mas_equalTo((topdis + parthit) * 2 + parthit + cxsingler.topdis + 44);
        make.left.mas_equalTo(20);
        make.height.mas_equalTo(30);
        make.right.mas_equalTo(-20);
    }];
    //    [_centerTime cx_eventsBlockHandler:^{
    //        [weakSelf pickerNow:weakSelf.centerTime];
    //    } forControlEvents:UIControlEventTouchDragInside];
    [_centerTime cx_eventsBlockHandler:^{
        [weakSelf pickerNow:weakSelf.centerTime];
    } forControlEvents:UIControlEventTouchUpInside];
    //    [_centerTime addTarget:self action:@selector(centerTimed:) forControlEvents:UIControlEventTouchUpInside];
//    _centerTime.backgroundColor = [UIColor cyanColor];
    [_centerTime setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    _centerTime.hidden = NO;
    
}

//-(void)centerTimed:(UIButton *)sender{
//    [self pickerNow:sender];
//}


-(void)pickerChange{
    _type = 1 - _type;
    if (_type == 0) {
        [_pickerBtn setTitle:@"按月选择>" forState:UIControlStateNormal];
        _centerTime.hidden = NO;
        
    }
    else if (_type == 1){
        [_pickerBtn setTitle:@"按日选择>" forState:UIControlStateNormal];
        _centerTime.hidden = YES;
    }
}

-(void)pickerNow:(UIButton *)sender{
    _currentTime = sender;
    if (!_pickerView) {
        _pickerView = [[UIButton alloc] initWithFrame:CGRectMake(0, cxsingler.topdis, cx_wid, cx_hit - cxsingler.topdis - cxsingler.bottomdis - 44)];
        
        UIView *bg = [[UIView alloc] initWithFrame:CGRectMake(0, cx_hit - 170 - cxsingler.bottomdis - cxsingler.topdis, cx_wid, 170)];
        bg.backgroundColor = [UIColor whiteColor];
        [_pickerView addSubview:bg];
        WEAKSELF;
        [_pickerView cx_eventsBlockHandler:^{
            weakSelf.pickerView.hidden = YES;
        } forControlEvents:UIControlEventTouchUpInside];
        _pickerView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.2];
        
        NSArray *parts = @[@"取消",@"确定"];
        for (NSInteger index = 0; index <parts.count ; index ++) {
            UIButton *part = [[UIButton alloc] initWithFrame:CGRectZero];
            [part setTitle:parts[index] forState:UIControlStateNormal];
            [part setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            part.titleLabel.font = [UIFont systemFontOfSize:16];
            [part cx_eventsBlockHandler:^{
                if (index == 0) {
                    _pickerView.hidden = YES;
                }
                else{
                    [weakSelf.picker startCallBack];
                    _pickerView.hidden = YES;
                }
            } forControlEvents:UIControlEventTouchUpInside];
            [bg addSubview:part];
            [part mas_makeConstraints:^(MASConstraintMaker *make) {
                if (index == 0) {
                    make.left.mas_equalTo(0);
                }
                else if (index == 1){
                    make.right.mas_equalTo(0);
                }
                make.top.mas_equalTo(0);
                make.size.mas_equalTo(CGSizeMake(70, 30));
            }];
        }
    }
    if (_picker) {
        [_picker removeFromSuperview];
    }
    
    
    //        _picker = [[QFDatePickerView alloc] initDatePackerWithResponse:^(NSString * str) {
    
    //        }];
    
    _picker = [[CXDataPickView alloc] init];
    _picker.delegate = self;
    _picker.frame = CGRectMake(0, cx_hit - 130 - cxsingler.bottomdis - cxsingler.topdis, cx_wid, 130);
    
    _picker.backgroundColor = [UIColor purpleColor];
    //        _picker.loc = [NSLocale localeWithLocaleIdentifier:@"zh"];
    [_picker setMinimumDate:[NSDate dateWithTimeIntervalSince1970:0]];
    [_picker setMaximumDate:[NSDate dateWithTimeIntervalSinceNow:0]];
    
    //        [_picker setDatePickerMode:UIDatePickerModeDate];
    //        [_picker addTarget:self action:@selector(dateChange:)forControlEvents:UIControlEventValueChanged];
    [_pickerView addSubview:_picker];
    
    //        [_picker setDatePickerMode:UIDatePickerModeCountDownTimer];
    //
    
    
    //        UIDatePicker *datePicker = [[UIDatePicker alloc]init];
    //        //自定义位置
    //        datePicker.frame = CGRectMake(0, cx_hit * 0.4-240, 414, 150);
    //        //设置背景颜色
    //        datePicker.backgroundColor = [UIColor greenColor];
    //        //datePicker.center = self.center;
    //        //设置本地化支持的语言（在此是中文)
    //        datePicker.locale = [NSLocale localeWithLocaleIdentifier:@"zh"];
    //        //显示方式是只显示年月日
    //        datePicker.datePickerMode = UIDatePickerModeDate;
    //        //放在盖板上
    //        [_pickerView addSubview:datePicker];
    
    
    [self.view addSubview:_pickerView];
    
    //        _picker.
    if ([sender isEqual:_centerTime]) {
        _picker.type = CustomDataPickViewTypeYearMonth;
    }
    else{
        _picker.type = CustomDataPickViewTypeYearMonthDay;
    }
    _picker.defaultSelectDate = [NSDate new];
    _pickerView.hidden = NO;
    [_picker show];
}

-(void)dateChange:(UIDatePicker *)picker{
    NSLog(@"changed into %@",picker.date);
}


#pragma mark--picker代理start
-(void)pickViewCallBack_year:(NSString *)year month:(NSString *)month day:(NSString *)day{
    if (_currentTime) {
        NSString *time = [NSString stringWithFormat:@"%@-%@-%@",year,month,day];
        [_currentTime setTitle:time forState:UIControlStateNormal];
//        _currentTime = nil;
    }
}

-(void)pickViewCallBack_year:(NSString *)year month:(NSString *)month{
    if (_currentTime) {
        NSString *time = [NSString stringWithFormat:@"%@-%@",year,month];
        [_currentTime setTitle:time forState:UIControlStateNormal];
//        _currentTime = nil;
    }
}
#pragma mark--picker代理end

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
